package xyz.xmh.pattern.behavioral.interpreterpattern;

/**
 * @author Administrator
 * 解释器模式演示类
 */
public class InterpreterPatternDemo {

    public static void main(String[] args) {
        Expression orExpression = getOrExpression();
        Expression andExpression = getAndExpression();

        System.out.println("John is male? " + orExpression.interpret("John"));
        System.out.println("Julie is a married women? " + andExpression.interpret("Married Julie"));
    }


    /**
     * 规则：Robert 和 John 是男性
     *
     * @return 表达式
     */
    private static Expression getOrExpression() {
        Expression robert = new TerminalExpression("Robert");
        Expression john = new TerminalExpression("John");
        return new OrExpression(robert, john);
    }

    /**
     * 规则：Julie 是一个已婚的女性
     *
     * @return 表达式
     */
    private static Expression getAndExpression() {
        Expression julie = new TerminalExpression("Julie");
        Expression married = new TerminalExpression("Married");
        return new AndExpression(julie, married);
    }
}
